/*
 */
package org.hook.showcase.base.clone;

/**
 * @author suwuqiang 2011-12-28
 */
public class DeepCloneCase {
	public static void main(String[] args) {
		DeepCloneObject o1 = new DeepCloneObject();
		DeepCloneObject o2 = new DeepCloneObject();
		System.out.println("o1:" + o1);
		System.out.println("o2:" + o2);
		System.out.println("--------------------");
		o1.names[0] = "a";
		o1.names[1] = "b";
		o2 = o1.clone();
		o2.names[0] = "c";
		o2.names[1] = "d";
		System.out.println("o1:" + o1);
		System.out.println("o2:" + o2);
		System.out.println("o1.names[0]:" + o1.names[0]);
		System.out.println("o1.names[1]:" + o1.names[1]);
		System.out.println("o2.names[0]:" + o2.names[0]);
		System.out.println("o2.names[1]:" + o2.names[1]);
	}
}

class DeepCloneObject implements Cloneable {
	public String[] names;

	public DeepCloneObject() {
		names = new String[2];
	}

	@Override
	protected DeepCloneObject clone() {
		DeepCloneObject o = null;
		try {
			o = (DeepCloneObject) super.clone();
			o.names = (String[]) names.clone(); // deep clone.
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return o;
	}
}